package com.dscyy.readutil;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class MadeHotdata {
	
	static Map<Integer, HourLimitPercentage> limtHourMap = null;
	static Map<Integer, HourLimitPercentage> limtMonthMap = null;
	
	static String fileSort = "1";
	static int useUserQty = 75;
	
//	static String[] fileSortarg = {"1", "2", "3", "4", "5"};
//	static int[] useUserQtyarg = {45, 76, 100, 49, 105};
	static String[] fileSortarg = {"25"};
	static int[] useUserQtyarg = {0};
	static String firstStartDate = "2018-06-15";
	static int randomDate = 16;
	

	public static void main(String[] args) throws Exception {
		int a = 1;
		if(a == 2) {
			System.out.println(calcStartDate());
		}else {
			List<String> allSql = new ArrayList<String>();
				List<String[]> allShop = readShop();// 全部店铺及商品
				List<String[]> allCurs = readAreas();// 读取地区
				DecimalFormat df = new DecimalFormat("#.00");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				for(String[] shop : allShop) {
					int records = Integer.parseInt(Math.round(Math.random() * 50)+"");
					for(int i = 0; i<records;i++) {
						String orderid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
						String platform = shop[0];
						String shopname = shop[2];
						String productname = shop[3];
						String tradetime = addTimeToEveryCurs(sdf.format(new Date()));
						String[] tradearea = allCurs.get(Integer.parseInt(Math.round(Math.random()*(allCurs.size()-2))+""));
						double tradeamount = Math.random()*10* (Double.parseDouble(shop[4]));
						tradeamount = Double.parseDouble(df.format(tradeamount));
						String sex = Math.round(Math.random()*100)%2==0?"男":"女";
						long age = Math.round(Math.random()*100);
						String category = shop[1];
						String sql = "insert into hot_data_info(orderid,platform,shop,productname,tradetime,tradearea,tradeamount,sex,age,category) values('"+
						orderid+"','"+platform+"','"+shopname+"','"+productname+"','"+tradetime+"','"+tradearea[0]+"',"+tradeamount+",'"+sex+"',"+age+",'"+category+"');";
						allSql.add(sql);
					}
				}
			writeSqlToFile(allSql, "C:\\Users\\dscyy\\Desktop\\seller.sql");
		}
		
	}
	
	private static void writeSqlToFile(List<String> allSql, String fileName) {
		try {
			FileWriter writer = new FileWriter(fileName);
			for(String sql : allSql) {
				writer.write(sql + "\n");
			}
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 给每次上课加个时间
	 * @param curses
	 * @param startDate
	 * @throws ParseException 
	 */
	private static String addTimeToEveryCurs(String startDate) throws ParseException {
		SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 得到随机的 时分秒
		int hour =  Integer.parseInt(Math.round(Math.random()*20)+"");
		startDate = startDate + " 00:00:00";
		Calendar c = Calendar.getInstance();
		c.setTime(f.parse(startDate));
		c.set(Calendar.HOUR_OF_DAY, hour);
		c.add(Calendar.MINUTE, Integer.parseInt(Math.round(Math.random()*60)+""));
		c.add(Calendar.SECOND, Integer.parseInt(Math.round(Math.random()*60)+""));
		return f.format(c.getTime());
	}

	private static String calcStartDate() throws ParseException {
		int mon = Integer.parseInt(Math.round(Math.random() * 12)+"");
		String year = "2017";
		String month = "";
		if(mon <= 6) {
			year = "2018";
			month = "0" + String.valueOf(mon);
		}else {
			year = "2017";
			if(mon>=10)
				month = String.valueOf(mon);
			else
				month = "0" + String.valueOf(mon);
				
		}
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		Date date = f.parse(year + "-" + month + "-01");
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_YEAR, Integer.parseInt(Math.round(Math.random() * 20)+""));
		if(mon == 6 || c.getTime().compareTo(new Date()) >= 0) {
			return calcStartDate();
		}
		return f.format(c.getTime());
	}

	public static List<String> filterNumsUser(List<String[]> allUser, int nums, int index){
		List<String> users = new ArrayList<String>();
		// 将用户列表整理成map
		Map<Integer, String[]> map = new HashMap<Integer, String[]>();
		int key = 1;
		for(String[] str : allUser) {
			map.put(key++, str);
		}
		// 随机取数
		int allCount = map.size();
		if(nums == 0) {
			nums = allCount;
		}
		for(int i=1;i <= nums; i++) {
			queryUserInMap(users, map, allCount, index);
		}
		return users;
	}
	
	private static void queryUserInMap(List<String> users, Map<Integer, String[]> map, int allCount, int index) {
		String strKey = Math.round(Math.random() * allCount)+"";
		Integer key = Integer.parseInt((strKey));
		if(map.containsKey(key)) {
			users.add(map.get(key)[index]);
			map.remove(key);
		}else {
			queryUserInMap(users, map, allCount, index);
		}
		
	}

	private static List<String[]> readShop() throws Exception{
		// 读取用户数据
		String filename = "C:\\Users\\dscyy\\Desktop\\商品信息.csv";
		String regex = ",";
		return readFiles(filename, regex);
	}
	
	private static List<String[]> readAreas() throws Exception{
		// 读取用户数据
		String filename = "C:\\Users\\dscyy\\Desktop\\地区.csv";
		String regex = ",";
		return readFiles(filename, regex);
	}	

	private static List<String[]> readFiles(String filename, String regex) throws Exception {
		List<String[]> lineList = new ArrayList<String[]>();
		InputStreamReader isr = new InputStreamReader(new FileInputStream(filename), "GB2312"); // 或GB2312,GB18030
		BufferedReader read = new BufferedReader(isr);
		String contentLine = read.readLine();
		while (contentLine != null) {
			lineList.add(contentLine.split(regex));
			contentLine = read.readLine();
		}
		read.close();
		isr.close();
		return lineList;
	}


}
